import { GameData } from "./GameData";
import { SocketMag, SocketType } from "./SocketMag";
import { TipMag } from "./TipsUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SetPanel extends cc.Component {

    public btn_sure: cc.Node = null;
    public btn_exit: cc.Node = null;
    public EditBox_msg: cc.EditBox = null;


    onLoad() {
        this.btn_sure = this.node.getChildByName('btn_sure');
        this.btn_exit = this.node.getChildByName('btn_exit');
        this.EditBox_msg = this.node.getChildByName('EditBox_msg').getComponent(cc.EditBox);
        this['lab_title'] = this.node.getChildByName('lab_title').getComponent(cc.Label);
    }

    start() {
        this['btn_sure'].on(cc.Node.EventType.TOUCH_END, () => {

            let str = this.EditBox_msg.string;
            if (!str) {
                return TipMag.In.PopTips('不能为空');
            };

            if (this.m_ncurSocketType === SocketType.setName) {
                SocketMag.getInstance().SendMsg({ name: str }, SocketType.setName);
            }
            else if (this.m_ncurSocketType === SocketType.wirteResule) {
                SocketMag.getInstance().SendMsg({ resule: str, roomid: GameData.Inst().m_nRoomid }, SocketType.wirteResule);
            }
            else if (this.m_ncurSocketType === SocketType.joinRoom) {
                SocketMag.getInstance().SendMsg({ roomid: parseInt(str), userid: GameData.Inst().m_nUserid }, SocketType.joinRoom);
            }
            else if (this.m_ncurSocketType === SocketType.sendResule) {
                SocketMag.getInstance().SendMsg({
                    roomid: GameData.Inst().m_nRoomid,
                    userid: GameData.Inst().m_nUserid,
                    text: str
                }, SocketType.sendResule);
            }
        }, this)


        this['btn_exit'].on(cc.Node.EventType.TOUCH_END, () => {
            this.node.destroy();
        }, this)
    }


    public m_ncurSocketType: SocketType = 0;
    public SwitchPage(ViewType: SocketType) {
        this.m_ncurSocketType = ViewType;
        this.btn_exit.active = false;
        this.btn_sure.x = 0;
        if (ViewType === SocketType.setName) {
            this['lab_title'].string = '请设置游戏昵称';
            this.EditBox_msg.inputMode = cc.EditBox.InputMode.SINGLE_LINE;
            this.EditBox_msg.placeholder = '请填写昵称';
        }
        else if (ViewType === SocketType.wirteResule) {
            this['lab_title'].string = '请设置答案';
            this.EditBox_msg.inputMode = cc.EditBox.InputMode.SINGLE_LINE;
            this.EditBox_msg.placeholder = '请填写答案'
        }
        else if (ViewType === SocketType.joinRoom) {
            this['lab_title'].string = '请输入房间号';
            this.EditBox_msg.inputMode = cc.EditBox.InputMode.NUMERIC;
            this.btn_exit.active = true;
            this.btn_sure.x = 125;
        }

    }
}
